<?php

namespace app\admin\controller;


use app\admin\model\BsMenuModel;
use app\admin\model\CommonModel;
use app\admin\model\HoteldataModel;
use app\admin\model\PlAreaModel;
use model\wayModel\cache\WProductCache;
use think\Session;

/**
 * 酒店列表
 * @date 2020年1月16日14:59:34
 * Class Hoteldata
 * @package app\admin\controller
 */
class Hoteldata extends Common
{

    public function hotelList()
    {
        $cache = WProductCache::instance();

        $translate = [
            'jd' => 'name_hk',
            'jd_e' => 'name_en',
            'dq' => 'area',
            'dj' => 'rankid',
            'dh' => 'phone',
            'cz' => 'fax',
            'dz' => 'dz',
            'rm' => 'is_hot',
            'zt' => 'state',
            'tjsj' => 'addtime',
        ];

        $where['h.is_delete'] = 1;

        $limit = empty( Session::get( 'limit', 'hotelData'))
            ? 10 : Session::get( 'limit', 'hotelData');

        $is_hidden = Session::get( 'ishidden', 'hotelData');

        !isset( $is_hidden) || $is_hidden == 1 ? $where['ishidden'] = 1 : $where;

        $id = Session::get( 'id', 'hotelData'); // 搜索类型ID
        $area = Session::get( 'area', 'hotelData');
        $hid = Session::get( 'hid', 'hotelData');
        $hotel = Session::get( 'hotel', 'hotelData');
        $cktype = Session::get( 'cktype', 'hotelData'); // 搜索类型
        $hotel_id = Session::get( 'hotelid', 'hotelData');
        $this->assign( 'des_id', $id);
        $this->assign( 'area', $area);
        $this->assign( 'hid', $hid);
        $this->assign( 'hotel', $hotel);
        $this->assign( 'cktype', $cktype);
        $this->assign( 'hotel_id', $hotel_id);

        $orders = Session::get( 'orderby', 'hotelData');
        if( !$orders)
        {
            $orders = 'h.id desc';
        }

        if( isset( $cktype))
        {
            switch ( $cktype)
            {
                case 'hotel' :
                    $where['h.id'] = empty( $hid) ? $id : $hid;
                    break;
                case 'area' :
                    // 获取地区所有子集
                    $area_ids = ( new PlAreaModel())->getChildrenID( $id);
                    $where['h.area_id'] = [ 'in', $area_ids];
                    break;
            }
        }

        if( !empty( $hotel_id))
        {
            $where['h.id'] = $hotel_id;
        }

        $hotel_list = ( new HoteldataModel())
            ->hotelList( $where, $limit, $orders); // 获取酒店列表

        $page = $hotel_list->render();

        $hotel_list = $hotel_list->toArray()['data'];
        foreach ( $hotel_list as $k => $v)
        {
            $area_arr = $cache->searchCache( $v['area_id'],
                'area', 0); //通过目的地id获取缓存中的目的地字符串

            if( !empty( $area_arr))
            {
                foreach ( $area_arr as $kk => $vv)
                {
                    $hotel_list[$k]['area'] = $vv[0]['zh_hk']['name']; // 追加area字段
                }
            }
            else
            {
                $hotel_list[$k]['area'] = '';
            }

            $hotel_list[$k]['dz'] = '';

            if( !empty( $hotel_list[$k]['address_en']) &&
                empty( $hotel_list[$k]['address_hk']))
            {   // 只有英文地址，则只显示英文地址
                $hotel_list[$k]['dz'] = $hotel_list[$k]['address_en'];
            }

            if( empty( $hotel_list[$k]['address_en']) &&
                !empty( $hotel_list[$k]['address_hk']))
            {   // 只有繁体地址则显示繁体地址
                $hotel_list[$k]['dz'] = $hotel_list[$k]['address_hk'];
            }

            if( !empty( $hotel_list[$k]['address_en']) &&
                !empty( $hotel_list[$k]['address_hk']))
            {   // 两者都有则显示两个地址
                $hotel_list[$k]['dz'] = $hotel_list[$k]['address_en'] .
                    ',' . $hotel_list[$k]['address_hk'];
            }
        }

        $url = CommonModel::getUrl(); // 获取方法绝对路径

        $order = BsMenuModel::getAdminSettingByUrl( $url); // 获取后台用户列排序设置

        $search_data = ( new CommonModel())->getSearchData( 'zh_hk',
            [ 'hotArea', 'hotel', 'area', 'default', 'groomCountry']); // 获取搜索框内容
//        echoJson( $search_data);

        $this->assign( 'searchData', $search_data);
        $this->assign( 'page', $page);
        $this->assign( 'ishidden', $is_hidden);
        $this->assign( 'order', $order);
        $this->assign( 'url', $url);
        $this->assign( 'limit', $limit);
        $this->assign( 'translate', $translate);
        $this->assign( 'orders', explode( " ", $orders));
        $this->assign( 'hotelLists', $hotel_list);

        return $this->fetch("/hotel/hotel-infor");

    }


























































}